//
//  GarbageListHeaderView.swift
//  Community
//
//  Created by MAC on 2019/8/2.
//  Copyright © 2019 ZKJ. All rights reserved.
//

import UIKit

class SWPTGarbageListHeaderView: UIView,NibLoadable {

    @IBOutlet weak var title: UILabel!
    @IBOutlet weak var content: UIView!
    private var collectionView: UICollectionView!
    
    override func awakeFromNib() {
        super.awakeFromNib()
         let layout = UICollectionViewFlowLayout.init()
//        layout.scrollDirection = .vertical
        collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: layout)
        collectionView.register(SWPTGarbageListHeaderCell.nib(), forCellWithReuseIdentifier: SWPTGarbageListHeaderCell.identifier)
        collectionView.delegate = self
        collectionView.dataSource = self
        self.content.addSubview(collectionView)
        collectionView.backgroundColor = UIColor.white
        collectionView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
    }
    
    var garbages:[SWPTGarbage]? {
        didSet {
           self.collectionView.reloadData()
        }
    }

}
extension SWPTGarbageListHeaderView: UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return garbages?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: SWPTGarbageListHeaderCell.identifier, for: indexPath) as! SWPTGarbageListHeaderCell
        cell.title.text = garbages![indexPath.row].name
        cell.iamge.image = UIImage(named: garbages![indexPath.row].image)
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let width = SCREEN_WIDTH / 4
        let height: CGFloat = 80
        return CGSize(width: width, height: height)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets.zero
    }
}
